/*
** DLNA ConnectionManager service
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <http_protocol.h>
#include <http_log.h>

#include <apr_tables.h>
#include <apr_pools.h>
#include <apr_xml.h>

#include "template.h"
#include "conn.h"

#define SERVICE_CONNECTION_MANAGER "urn:schemas-upnp-org:service:ConnectionManager:1"
#define CONNECTION_ACTION_PROTOINFO	"#GetProtocolInfo"

static int
conn_protoinfo(request_rec *r)
{
	ap_set_content_type(r, "text/xml; charset=utf-8");

	ap_rputs(template_protoinfo,
	    r);

	return OK;
}

int
dlna_conn(request_rec *r)
{
	const char* action = apr_table_get(r->headers_in, "SOAPACTION");
	if (action != NULL) {
		while (*action == '\"' || *action == '\'')
			++action;

		if (! strncmp(action, SERVICE_CONNECTION_MANAGER, strlen(SERVICE_CONNECTION_MANAGER))) {
			action += strlen(SERVICE_CONNECTION_MANAGER);
			if (! strncmp(action, CONNECTION_ACTION_PROTOINFO, strlen(CONNECTION_ACTION_PROTOINFO)))
				return conn_protoinfo(r);
		}
	}

	action = (const char*)apr_table_get(r->headers_in, "SOAPACTION");
	ap_log_error(APLOG_MARK, APLOG_ERR|APLOG_NOERRNO, 0, r->server,
	    "Cannot parse SOAPACTION: %s",
	    action);

	return HTTP_INTERNAL_SERVER_ERROR;
}
